// Copyright 2010 Google Inc. All Rights Reserved.

package com.google.zahra.webmanager;

import com.google.zahra.testmanager.TestManager;
import com.google.zahra.workmanager.ZahraWorkManager;

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.terracotta.workmanager.pipemanager.PipeManager;

/**
 * The embedded Jetty web server
 * @author arthurkok@google.com (Your Name Here)
 *
 */

public class WebManager extends Thread {
  
  protected static TestManager TEST_MANAGER;
  protected static PipeManager<String> PIPE_MANAGER;
  protected static ZahraWorkManager WORK_MANAGER;
  
  public WebManager(TestManager testManager, PipeManager<String> pipeManager, ZahraWorkManager workManager) {
    WebManager.TEST_MANAGER = testManager;
    WebManager.PIPE_MANAGER = pipeManager;
    WebManager.WORK_MANAGER = workManager;
  }
  
  @Override
  public void run() {
    System.out.println("Starting web server");
    Server server = new Server(8080);
    ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
    context.setContextPath("/");
    server.setHandler(context);

    // Add the servlets
    context.addServlet(new ServletHolder(new MainServlet()), "/*");
    context.addServlet(new ServletHolder(new XmlRpcService()), "/addTest");
    context.addServlet(new ServletHolder(new TestServlet()), "/viewTest");
    context.addServlet(new ServletHolder(new WorkerServlet()), "/workerInfo");
    context.addServlet(new ServletHolder(new AllTestServlet()), "/allTest");
    context.addServlet(new ServletHolder(new SubmitTestServlet()), "/submitTest");
    context.addServlet(new ServletHolder(new StatisticsServlet()), "/stats");
    context.addServlet(new ServletHolder(new WorkManagerServlet()), "/workManager");
    
    try {
      server.start();
      server.join();
    } catch (Exception e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }
}
